package com.zxl.gtion.controller;

import com.zxl.gtion.entity.*;
import com.zxl.gtion.enums.RedisEnums;
import com.zxl.gtion.service.*;
import com.zxl.gtion.util.R;
import com.zxl.gtion.vo.CityVO;
import com.zxl.gtion.vo.ProvinceVO;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: bo-feifei
 * @Date 2024/3/22 12:12
 * @Version 1.0
 */
@RestController
@RequestMapping("city")
@RequiredArgsConstructor
public class CityController {

    private final CityService cityService;
    private final DistrictService districtService;
    private final ProvinceService provinceService;
    private final ProCityService proCityService;
    private final CityDistrictService cityDistrictService;
    private final StringRedisTemplate redisTemplate;

    @GetMapping("cityMap")
    @ApiOperation("获取各城市经纬度信息")
    public R<List<ProvinceVO>> cityMap() {
        List<Province> provinces = provinceService.list();
        if (provinces.isEmpty()) {
            return R.ok();
        }
        List<ProvinceVO> provinceVOS = new ArrayList<>();
        ProvinceVO provinceVO;
        for (Province province : provinces) {
            Integer id = province.getId();
            provinceVO = new ProvinceVO();
            provinceVO.setName(province.getProvince());

            List<ProCity> proCities = proCityService.lambdaQuery().eq(ProCity::getProId, id).list();
            if (proCities.isEmpty()) {
                continue;
            }
            int i = 0;
            List<CityVO> cityVOS = new ArrayList<>();
            CityVO cityVO;
            for (ProCity proCity : proCities) {
                Integer cityId = proCity.getCityId();
                City city = cityService.getById(cityId);
                cityVO = new CityVO();
                cityVO.setName(city.getCity());

                List<CityDistrict> cityDistricts = cityDistrictService.lambdaQuery().eq(CityDistrict::getCityId, city.getId()).list();
                if (cityDistricts.isEmpty()) {
                    i++;
                    continue;
                }
                List<District> districts = new ArrayList<>();
                for (CityDistrict cityDistrict : cityDistricts) {
                    Integer districtId = cityDistrict.getDistrictId();
                    District dis = districtService.getById(districtId);
                    if (dis != null) {
                        districts.add(dis);
                    }
                }
                if (!districts.isEmpty()) {
                    cityVO.setDistricts(districts);
                }
                cityVOS.add(cityVO);
            }

            if (i == proCities.size()) {
                continue;
            }
            if (!cityVOS.isEmpty()) {
                provinceVO.setCityVOS(cityVOS);
            }
            provinceVOS.add(provinceVO);
        }
        return R.ok(provinceVOS);
    }


    @GetMapping("insertCityInfo")
    @Transactional(rollbackFor = Exception.class)
    public R insertCityInfo() {
//        Province province = new Province();
//        province.setProvince("广东省");
//        provinceService.save(province);
//        City city = new City();
//        city.setCity("广州市");
//        cityService.save(city);
//        ProCity proCity = new ProCity();
//        proCity.setCityId(city.getId());
//        proCity.setProId(province.getId());
//        proCityService.save(proCity);
        City city = cityService.lambdaQuery().eq(City::getCity, "广州市").one();
        if (city == null) {
            city = new City();
            city.setCity("广州市");
            cityService.save(city);
        }
        District district = new District();
        district.setDistrict("从化区");
        district.setLa("113.677267");
        district.setLon("23.629149");
        districtService.save(district);
        CityDistrict cityDistrict = new CityDistrict();
        cityDistrict.setCityId(city.getId());
        cityDistrict.setDistrictId(district.getId());
        cityDistrictService.save(cityDistrict);

        return R.ok();
    }

    @GetMapping("insertCityInfo2")
    @Transactional(rollbackFor = Exception.class)
    public R insertCityInfo2() {

        City city = cityService.lambdaQuery().eq(City::getCity, "广州市").one();
        if (city == null) {
            city = new City();
            city.setCity("广州市");
            cityService.save(city);
        }
        District district = new District();
        district.setDistrict("越秀区");
        district.setLa("113.817078");
        district.setLon("23.268303");
        districtService.save(district);
        CityDistrict cityDistrict = new CityDistrict();
        cityDistrict.setCityId(city.getId());
        cityDistrict.setDistrictId(district.getId());
        cityDistrictService.save(cityDistrict);

        return R.ok();
    }
}
